package com.example.a10928.weifriend.http.configuration;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.example.a10928.weifriend.App.app;


/**
 * Created by Chuang on 2018/3/1.
 * http的拦截器,将截取的数据进行打包
 */

public class DataParamsInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        RequestBody body = request.body();
        if ("GET".equals(request.method())) {

        } else if ("POST".equals(request.method())) {

//            if (body instanceof FormBody) {
//                FormBody formBody = (FormBody) body;
//                FormBody.Builder builder = new FormBody.Builder();
//                for (int i = 0; i < formBody.size(); i++) {
//                    String value = formBody.value(i);
//                    builder.add(formBody.name(i), toEncodedJson(value));
//                }
//                FormBody newBody = builder.build();
//                request = request.newBuilder()
//                        .post(newBody)
//                        .build();
//            }
        }
        return chain.proceed(request);
    }

    private String toEncodedJson(String value) throws UnsupportedEncodingException {
        String json = app().getGson().toJson(value);
//        return URLEncoder.encode(json, "utf-8");
        return json;
    }
}
